package app.util

import org.apache.log4j.Logger
import org.jooq.tools.json.JSONObject
import spark.Request
import java.text.SimpleDateFormat
import java.util.*


object Log {
    private val logger = Logger.getLogger(Log::class.java)

    /**
     * 返回Logger对象
     */
    fun getInstance(): Logger{
        return logger
    }

    /**
     * 记录用户请求信息
     */
    fun ua(req: Request) {
        val currentTime = Date()
        val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val query_params = req.queryParams()
        val json_params = JSONObject()
        query_params.forEachIndexed { index, s ->
            json_params.put(s, req.queryParams(s))
        }
        logger.info("======================================================================================================")
        logger.info("请求路径：" + req.url())
        logger.info("访问ip：" + req.ip())
        logger.info("访问时间：" + formatter.format(currentTime))
        logger.info("访问参数：" + json_params)
    }
}